define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'recharge/index' + location.search,
                    multi_url: 'recharge/multi',
                    table: 'recharge',
                },
                showExport:false
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                var countData = data.extend.count;
                $('#tj').remove();
                var html = '<div id="tj" class="row" style="padding-top: 10px">';
                $.each(countData,function (index,value) {
                    html = html +  '<div class="table-dashboard-item-container form-group col-xs-12 col-sm-6 col-md-2 col-lg-2"><div class="table-dashboard-item-tip"><span class="table-dashboard-item-dot"></span>'+value.name+':</div>'+value.value+'</div>';
                });
                html = html+'</div>'
                $(".fixed-table-body").append(html);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: true,
                exportTypes: ['excel'],
                exportOptions: {
                    ignoreColumn: [0, 'operate'],
                    fileName: '商家充值明细_'+ Moment().format("YYYY-MM-DD"),
                    onBeforeSaveToFile: function (data, fileName, type, charset, encoding, bom) {
                        return $.fn.bootstrapTable.defaults.extend.savestatus;
                    },
                    onCellHtmlHyperlink: function ($cell, rowIndex, colIndex, href, htmlData) {
                        var html = $.parseHTML(htmlData);
                        var inputidx = 0;
                        var selectidx = 0;
                        var result = '';

                        $.each(html, function () {
                            if ($(this).is("input")) {
                                result += $cell.find('input').eq(inputidx++).val();
                            } else if ($(this).is("select")) {
                                result += $cell.find('select option:selected').eq(selectidx++).text();
                            } else if ($(this).is("img")) {
                                var src = $(this).attr('src');
                                if (src.indexOf('http') === -1) {
                                    $(this).attr('src', window.location.origin + src);
                                }
                                $(this).attr("width", "40").attr("height", "40").attr('class', '');
                                result += '<div>' + $(this).get(0).outerHTML + '</div>';
                            } else {
                                if (typeof $(this).html() === 'undefined') {
                                    result += $(this).text();
                                } else if (jQuery().bootstrapTable === undefined ||
                                    ($(this).hasClass('filterControl') !== true &&
                                        $cell.parents('.detail-view').length === 0)) {
                                    result += $(this).html();
                                }
                            }
                        });
                        return result;
                    }
                },
                columns: [
                    [
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'shop.shop_name', title: '商家名称'},
                        {field: 'agents.nickname', title: '所属代理商'},
                        {field: 'money', title: __('Money'),operate:'BETWEEN',sortable: true,},
                        {field: 'status', title: '状态', formatter: Table.api.formatter.status, custom:{0: '等待代理商充值', 1: '等待平台充值',2:'等待商家确认',3:'充值完成'},searchList: {0: '等待代理商充值', 1: '等待平台充值',2:'等待商家确认',3:'充值完成'}},
                        {field: 'createtime', title: '申请时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'agent_record', title: '代理充值记录', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'agenttime', title: '代理充值时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,},
                        {field: 'platform_record', title: '平台充值记录', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'platformtime', title: '平台充值时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: '充值',
                                    text: '充值',
                                    title: '充值',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-dialog',
                                    url: 'recharge/confirm',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 1) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click"); //刷新数据
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        confirm: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
